/*
 * @Author: 刘润泽
 * @Date: 2022-11-08 18:12:27
 * @LastEditors: 刘润泽
 * @LastEditTime: 2023-03-14 20:04:51
 * @Description: 针对表【autologous_blood_information】的数据库操作Service实现
 */
package server.business.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import server.business.pojo.AutologousBloodInformation;
import server.business.service.AutologousBloodInformationService;
import server.business.mapper.AutologousBloodInformationMapper;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("autologousBloodInformationService")
public class AutologousBloodInformationServiceImpl
                extends ServiceImpl<AutologousBloodInformationMapper, AutologousBloodInformation>
                implements AutologousBloodInformationService {
        /**
         * 装配数据库查询
         */
        @Autowired
        private AutologousBloodInformationMapper autologousBloodInformationMapper;


        /**
         * 更改血液信息
         * @param bloodInformation
         */
        @Override
        public void updateBloodInfo(AutologousBloodInformation autologousBloodInformation) {
                LambdaUpdateWrapper<AutologousBloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(Strings.isNotBlank(autologousBloodInformation.getBloodId()), AutologousBloodInformation::getBloodId, autologousBloodInformation.getBloodId());
                updateWrapper.set(Strings.isNotBlank(autologousBloodInformation.getBloodComponent()), AutologousBloodInformation::getBloodComponent, autologousBloodInformation.getBloodComponent())
                        .set(Strings.isNotBlank(autologousBloodInformation.getAboBloodType()), AutologousBloodInformation::getAboBloodType, autologousBloodInformation.getAboBloodType())
                        .set(Strings.isNotBlank(autologousBloodInformation.getRhBloodType()), AutologousBloodInformation::getRhBloodType, autologousBloodInformation.getRhBloodType())
                        .set(autologousBloodInformation.getBloodCount() != null, AutologousBloodInformation::getBloodCount, autologousBloodInformation.getBloodCount())
                        .set(Strings.isNotBlank(autologousBloodInformation.getBloodLocation()), AutologousBloodInformation::getBloodLocation, autologousBloodInformation.getBloodLocation())
                        .set(Strings.isNotBlank(autologousBloodInformation.getState()), AutologousBloodInformation::getState, autologousBloodInformation.getState());
                autologousBloodInformationMapper.update(null, updateWrapper);
        }
        /**
         * 分页查询
         * 
         * @param currentPage
         * @param pageSize
         * @param bloodInformation
         * @return
         */
        @Override
        @SuppressWarnings("")
        public IPage<AutologousBloodInformation> getPage(int currentPage, int pageSize,
                                                         AutologousBloodInformation autologousBloodInformation) {
                LambdaQueryWrapper<AutologousBloodInformation> queryWrapper = new LambdaQueryWrapper<AutologousBloodInformation>();
                queryWrapper
                        .eq(Strings.isNotBlank(autologousBloodInformation.getPatientIdcard()),
                                AutologousBloodInformation::getPatientIdcard,
                                autologousBloodInformation.getPatientIdcard())
                        .eq(Strings.isNotBlank(autologousBloodInformation.getBloodId()),
                                AutologousBloodInformation::getBloodId,
                                autologousBloodInformation.getBloodId())
                        .eq(Strings.isNotBlank(autologousBloodInformation.getState()),
                                AutologousBloodInformation::getState,
                                autologousBloodInformation.getState())
                        .eq(Strings.isNotBlank(autologousBloodInformation.getAboBloodType()),
                                AutologousBloodInformation::getAboBloodType,
                                autologousBloodInformation.getAboBloodType())
                        .eq(Strings.isNotBlank(autologousBloodInformation.getRhBloodType()),
                                AutologousBloodInformation::getRhBloodType,
                                autologousBloodInformation.getRhBloodType())
                        .eq(Strings.isNotBlank(autologousBloodInformation.getBloodComponent()),
                                AutologousBloodInformation::getBloodComponent,
                                autologousBloodInformation.getBloodComponent())
                        .eq(autologousBloodInformation.getBloodCount() != null,
                                AutologousBloodInformation::getBloodCount,
                                autologousBloodInformation.getBloodCount())
                        .eq(Strings.isNotBlank(autologousBloodInformation.getBloodLocation()),
                                AutologousBloodInformation::getBloodLocation,
                                autologousBloodInformation.getBloodLocation())
                        .le(autologousBloodInformation.getCollectTime() != null,
                                AutologousBloodInformation::getCollectTime,
                                autologousBloodInformation.getCollectTime())
                        .ge(autologousBloodInformation.getFailureTime() != null,
                                AutologousBloodInformation::getFailureTime,
                                autologousBloodInformation.getFailureTime());
                IPage<AutologousBloodInformation> page = new Page<>(currentPage, pageSize);
                autologousBloodInformationMapper.selectPage(page, queryWrapper);
                return page;
        }

        /**
         * 根据自体输血表获得自体血信息表的信息
         *
         * @param transfusionSurgey
         * @return
         */
        @Override
        public AutologousBloodInformation getAutologousBloodInfoByTransfusionAuto(
                AutologousBloodInformation autologousBloodInformation) {
                LambdaQueryWrapper<AutologousBloodInformation> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper
                        .eq(Strings.isNotBlank(autologousBloodInformation.getPatientIdcard()),
                                AutologousBloodInformation::getPatientIdcard,
                                autologousBloodInformation.getPatientIdcard())
                        .eq(Strings.isNotBlank(autologousBloodInformation.getBloodComponent()),
                                AutologousBloodInformation::getBloodComponent,
                                autologousBloodInformation.getBloodComponent())
                        .ge(autologousBloodInformation.getBloodCount() != null,
                                AutologousBloodInformation::getBloodCount,
                                autologousBloodInformation.getBloodCount())
                        .le(autologousBloodInformation.getCollectTime() != null, AutologousBloodInformation::getCollectTime,
                                autologousBloodInformation.getCollectTime())
                        .ge(autologousBloodInformation.getFailureTime() != null, AutologousBloodInformation::getFailureTime,
                                autologousBloodInformation.getFailureTime())
                        .eq(AutologousBloodInformation::getState, "2"); // 2代表在库
                queryWrapper.select().orderByDesc(AutologousBloodInformation::getFailureTime);
                queryWrapper.last("limit 0,1");
                AutologousBloodInformation result = autologousBloodInformationMapper.selectOne(queryWrapper);
                return result;
        }

        /**
         * 根据血液id更改血液状态
         * 
         * @param bloodId
         * @param state
         */
        @Override
        public void setStateByBloodId(String bloodId, String state) {
                LambdaUpdateWrapper<AutologousBloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(bloodId != null, AutologousBloodInformation::getBloodId, bloodId);
                updateWrapper.set(AutologousBloodInformation::getState, state);
                autologousBloodInformationMapper.update(null, updateWrapper);
        }

        /**
         * 根据编号批量下载选中数据
         * 
         * @param response
         * @param ids
         */
        @Override
        public void batchDownload(HttpServletResponse response, ArrayList<Long> ids) throws IOException {
                List<AutologousBloodInformation> autologousBloodInformations = autologousBloodInformationMapper
                                .selectBatchIds(ids);
                // 设置文本内省
                try {
                        response.setContentType("application/vnd.ms-excel");
                        String fileName = URLEncoder.encode("自体血液信息" + System.currentTimeMillis(), "UTF-8");
                        // 设置字符编码
                        response.setCharacterEncoding("utf-8");
                        // 设置响应头
                        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
                        EasyExcel.write(response.getOutputStream(),
                                        AutologousBloodInformation.class).sheet("自体血液信息")
                                        .doWrite(autologousBloodInformations);
                        System.out.println(response);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}
